<?php
/*
	会员管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'onemid'=>0,'is_virtual'=>0);
	if($_RQ['id']){
		$params['id']=intval($_RQ['id']);
		$return['params']['id']=$_RQ['id'];
	}
	if($_RQ['fromuser']){
		$params['fromuser']=intval($_RQ['fromuser']);
		$return['params']['fromuser']=$_RQ['fromuser'];
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'createtime desc',
	);
	$list=pdo_getlist('core_members',$parameter,$total);
	if($list){
		$get_ostitle=get_ostitle();
		foreach($list as $k=>&$item){
			$item['member']=cfc('core')->member_show($item['id']);
			if(!$item['member']['id']){
				pdo_delete('jy_leijifee_member',array('mid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));	
				unset($list[$k]);
			}
			$item['agent']=cfc('core')->member_show($item['fromuser'],'',array('nickname'=>'总店'));
			$item['from_os']=$item['from_os']?$get_ostitle[$item['from_os']]:'微信';
			$member=pdo_get('jy_leijifee_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['id']));
			if(!$member['id']){
				//$this->model('member')->register(array('mid'=>$item['id']));
			}
			$item['is_agent']=$member['is_agent']?:0;
			$item['level_id']=$member['level_id']?:0;
			$item['nocheck']=$member['nocheck'];
			$item['myorder_price']=pdo_getsum('jy_leijifee_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['id']),'price')?:0;
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','tip'=>'<red>[温馨提示]：关于匿名用户的解释：正常认证服务号，没有关闭获取粉丝详细的情况下，是不会存在匿名用户的；可能小程序出现匿名用户的情况居多：原因是：小程序官方不允许首次访问弹窗获取粉丝详细。但是为了上下级的准确性，在访问小程序首页的时候就产生了下级关系！这时候就会出现匿名用户的情况。但是只要这个匿名用户访问了购物车，商品，会员中心等任何页面。就会弹窗获取粉丝信息。这时候匿名用户就会变成带昵称头像的会员了！</red>','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'id','title'=>'ID','type'=>'string'),
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'agent','title'=>'推荐人','type'=>'member'),
			array('name'=>'from_os','title'=>'来源','type'=>'string'),
			array('name'=>'is_agent','title'=>'是否分销商','type'=>'radio','options'=>'types'),
			array('name'=>'level_id','title'=>'分销商等级','type'=>'select','options'=>'levels'),
			array('name'=>'myorder_price','title'=>'自购金额','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'href','op'=>'change','title'=>'修改推荐人'),
		)),
		'search'=>array(
			array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'id','title'=>'选择会员'),
			array('name'=>'fromuser','width'=>15,'title'=>'推荐人ID','type'=>'string'),
		),
	);
	$return['htmls']=$htmls;
	$return['agent_status']=$this->agent_status;
	$return['levels']=$this->getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels']['0']=array('id'=>'0','title'=>'默认等级');
	$return['types']=array(
		'0'=>'普通会员',
		'1'=>'分销商',
	);
	exi($return);
}
if($_RQ['op']=='change'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$mid=intval($post['mid']);
		$agent_id=intval($post['agent_id']);
		$this->model('member')->set_parent($mid,$agent_id,true);
		exi('修改推荐人成功！','','list');
	}
	$item=$this->get('member',array('mid'=>intval($_RQ['id'])),array('mid','agent_id'));
	if(!$item){
		exi('指定会员不存在！','error');
	}
	$item['agent_id']=pdo_getsum('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>intval($_RQ['id'])),'fromuser');
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'修改推荐人',
		'edit'=>array(
			array('type'=>'choose','name'=>'agent_id','title'=>'设置推荐人','action'=>'core/member.get_member/','help'=>'填写0表示设置为总店'),
		),
	);
	exi($return);
}
if($_RQ['op']=='set'){
	$mid=intval($_RQ['id']);
	if(!$mid){
		exi('参数错误！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['nocheck'])){
		$update['nocheck']=intval($post['nocheck']);
	}
	if($update){
		pdo_update('jy_leijifee_member',$update,array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
?>